package lxs.tool.liberator.work.service.imp;

import lxs.tool.liberator.common.base.result.Response;
import lxs.tool.liberator.common.utils.IDCreator;
import lxs.tool.liberator.work.core.CodeMan;
import lxs.tool.liberator.work.mapper.InterfaceMapper;
import lxs.tool.liberator.work.mapper.ProjectMapper;
import lxs.tool.liberator.work.param.out.InterfaceOut;
import lxs.tool.liberator.work.pojo.Interface;
import lxs.tool.liberator.work.pojo.Project;
import lxs.tool.liberator.work.service.InterfaceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service("InterfaceServiceImp")
public class InterfaceServiceImp implements InterfaceService {
    @Autowired
    private InterfaceMapper interfaceMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CodeMan codeMan;

    @Override
    public Response showTable() throws Exception {
        Project mainProject = projectMapper.getMainProject();
        if (mainProject == null) {
            throw new Exception("请先设置主项目");
        }
        List<InterfaceOut> interfaceOuts = interfaceMapper.showTable(mainProject.getId());
        return Response.send(interfaceOuts);
    }

    @Override
    public Response getById(String id) {
        return Response.send(interfaceMapper.selectById(id));
    }

    @Override
    public Response deleteById(String id) {
        interfaceMapper.deleteById(id);
        return Response.success();
    }

    @Override
    public Response insert(Interface in) {
        in.setId(IDCreator.get("interface"));
        in.setProjectId(projectMapper.getMainProject().getId());
        try {
            interfaceMapper.insert(in);
        } catch (Exception e) {
            return Response.error("同一个表的接口名称重复");
        }
        return Response.success();
    }

    @Override
    public Response update(Interface in) {
        interfaceMapper.updateById(in);
        return Response.success();
    }


    @Override
    public Response getOptions() {
        return Response.send(interfaceMapper.getOptions(projectMapper.getMainProject().getId()));
    }

    @Override
    public Response download(HttpServletResponse response, String id) throws Exception {
        File file = codeMan.downloadInterfaceJava(id);
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setContentType("application/zip;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
        int read = inputStream.read();
        while (read != -1) {
            outputStream.write(read);
            outputStream.flush();
            read = inputStream.read();
        }
        inputStream.close();
        return Response.success("下载成功");
    }
}
